package io.litchi.auth.interfaces.facade.noAuth;

import io.litchi.auth.Infrastructure.common.result.login.RefreshTokenResult;
import io.litchi.auth.application.service.NoAuthService;
import io.litchi.auth.domain.entity.auth.User;
import io.litchi.auth.domain.repository.UserRepository;
import io.litchi.common.anno.mysql.QuerySql;
import io.litchi.common.anno.mysql.canstant.QueryMode;
import io.litchi.common.anno.mysql.handler.QueryUtil;
import io.litchi.common.result.Result;
import io.litchi.starter.oss.LitchiOssClient;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * description: 开放接口
 *
 * @author helang
 * @since 2025-09-11
 */
@RequestMapping("/noAuth")
@RestController
@Slf4j
@AllArgsConstructor
public class NoAuthFacade {

    private final NoAuthService noAuthService;

    private final LitchiOssClient litchiOssClient;

    private final UserRepository userRepository;

    /**
     * 刷新token 前端出现401 优先调改接口刷新token 如果该方法也返回401再路由至登录页
     *
     * @param request httpRequest
     * @return 认证信息 只包含token 和 refreshToken 以及对应的过期时间
     */
    @GetMapping("/refreshToken")
    public Result<RefreshTokenResult> refreshToken(HttpServletRequest request) {
        return noAuthService.refreshToken(request);
    }


    /**
     *
     * 上传文件 获取临时url
     *
     * @param file file
     * @return tempUrl
     * @throws IOException ex
     */
    @GetMapping("/testOss")
    public Result<String> testOss(@NotNull @RequestParam("file") MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String fileName = UUID.randomUUID().toString().replace("-", "") + "." + file.getOriginalFilename().split("\\.")[1];
        String key = litchiOssClient.upload(fileName, inputStream, file.getSize(), file.getContentType());
        String templateUrl = litchiOssClient.getTemporaryUrl(key, 60 * 60);
        return Result.success(templateUrl);
    }

    /**
     * 测试图片上传oss
     * @param bucketName bucketName
     * @return string
     */
    @GetMapping("/createBucket/{bucketName}")
    public Result<String> createBucket(@PathVariable("bucketName") String bucketName) {
        litchiOssClient.createBucketIfNotExists(bucketName);
        return Result.success();
    }

    /**
     *
     * 测试querySql
     * @param userParam 测试参数
     * @return void
     */
    @PostMapping("/test/querySql")
    public Result<Void> testQuerySql(@RequestBody UserParam userParam) {
        Specification<User> userSpecification = QueryUtil.buildQueryJpa(userParam, User.class);
        Page<User> all = userRepository.findAll(userSpecification, PageRequest.of(0, 10));
        log.info("查询结果:{}",all.getContent());
        log.info("条数:{}",all.getTotalElements());
        return Result.success();
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class UserParam {
        @QuerySql(value = "username", queryMode = QueryMode.EQ)
        private String username;

        @QuerySql(value = "nickname", queryMode = QueryMode.LIKE)
        private String nickname;
    }
}